<?php
include_once "header.php";

if(!isset($_GET["action"]))
{
    $_GET["action"] = "init";
}

if($_GET["action"] == "init")
{
    // create curl resource
    $ch = curl_init();
    // set url
    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/we/");
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // $output contains the output string
    $output = curl_exec($ch);
    // close curl resource to free up system resources
    curl_close($ch);

    $json_obj = json_decode($output);

    echo "<ol class='breadcrumb'>";
    echo "<li><a href='client.php'>Home</a></li>";
    echo "</ol>";

    foreach($json_obj->data as $continent)
    {
        // create curl resource
        $ch = curl_init();
        // set url
        curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/we/continent/countryCount/" . $continent->id);
        //return the transfer as a string
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        // $output contains the output string
        $output = curl_exec($ch);
        $output = json_decode($output);
        // close curl resource to free up system resources
        curl_close($ch);

        echo "<div class='panel panel-default'>";
        echo "<div class='panel-heading'><a href='client.php?action=getCountriesOnContinent&id=" . $continent->id . "'>$continent->name</a></div>";
        echo "<div class='panel-body'>";
        echo "Number of countries: " . $output->data;
        echo "</div>";
        echo "</div>";
    }
}
else if($_GET["action"] == "getCountriesOnContinent")
{
    // create curl resource
    $ch = curl_init();
    // set url
    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/we/continent/" . $_GET["id"]);
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // $output contains the output string
    $output = curl_exec($ch);
    // close curl resource to free up system resources
    curl_close($ch);

    $json_obj = json_decode($output);

    $continent_name = getContinentNameFromID($_GET["id"]);

    echo "<ol class='breadcrumb'>";
    echo "<li><a href='client.php'>Home</a></li>";
    echo "<li class='active'>$continent_name</li>";
    echo "</ol>";

    foreach($json_obj->data as $country)
    {
        echo "<div class='panel panel-default'>";
        echo "<div class='panel-heading'><a href='client.php?action=getCitiesInCountry&id=" . $country->id . "'>$country->name</a></div>";
        echo "<div class='panel-body'>";
        echo "Population: " . $country->population;
        if($country->capital == null)
        {
            echo "<br />Capital: undefined";
        }
        else
        {
            echo "<br />Capital: " . $country->capital->name;
        }
        echo "</div>";
        echo "</div>";
    }
}
else if($_GET["action"] == "getCitiesInCountry")
{
    // create curl resource
    $ch = curl_init();
    // set url
    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/we/country/" . $_GET["id"]);
    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // $output contains the output string
    $output = curl_exec($ch);
    // close curl resource to free up system resources
    curl_close($ch);

    $json_obj = json_decode($output);

    $country =  getContryNameFromID($_GET["id"]);
    $continent_id = $country->continent_id;
    $continent_name = getContinentNameFromID($continent_id);

    echo "<ol class='breadcrumb'>";
    echo "<li><a href='client.php'>Home</a></li>";
    echo "<li><a href='client.php?action=getCountriesOnContinent&id=" . $continent_id ."'>$continent_name</a></li>";
    echo "<li class='active'>$country->name</li>";
    echo "</ol>";

    if(count($json_obj->data) == 0)
    {
        echo "No cities defined";
    }

    foreach($json_obj->data as $city)
    {
        echo "<div class='panel panel-default'>";
        echo "<div class='panel-heading'>$city->name</a> <a href='client.php?action=deleteCity&id=$city->id' class='btn btn-danger' style='font-size:8pt;'>Delete</a></div>";
        echo "<div class='panel-body'>";
            echo "Population: " . $city->population;
        echo "</div>";
        echo "</div>";
    }
}
else if($_GET["action"] == "deleteCity")
{
    $city_id = $_GET["id"];

    $ch = curl_init("http://127.0.0.1/we/city/" . $city_id);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");

    $response = curl_exec($ch);
    header("location: /we/client.php");
}

function getContryNameFromID($id)
{
    $ch = curl_init();

// set url
    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/we/country/countryInformation/" . $id);

//return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
    $output = curl_exec($ch);

// close curl resource to free up system resources
    curl_close($ch);

    $json_obj = json_decode($output);

    return $json_obj->data;
}

function getContinentNameFromID($id)
{
    $ch = curl_init();

// set url
    curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/we/continent/continentInformation/" . $id);

//return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
    $output = curl_exec($ch);

// close curl resource to free up system resources
    curl_close($ch);

    $json_obj = json_decode($output);

    return $json_obj->data->name;
}
//------------------CITY LIST:
/*
// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, "http://127.0.0.1/we/country/f0_193");

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);


$json_obj = json_decode($output);
echo "<pre>";
echo json_encode($json_obj, JSON_PRETTY_PRINT);
echo "</pre>";
*/

/*
//------------------CITY INSERT:
$data = array(
    "name" => "AwesomeTown",
    "population"  => "2",
    "country" => "f0_193");

$ch = curl_init("http://127.0.0.1/we/city/f0_99999");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

$response = curl_exec($ch);

var_dump($response);
*/

/*
//------------------CITY DELETE:
$ch = curl_init("http://127.0.0.1/we/city/f0_99999");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");

$response = curl_exec($ch);

var_dump($response);



//------------------CITY POST/UPDATE:
/*
$data = array(
    "name" => "Kopenhagen",
    "population"  => "1358540",
    "country" => "f0_193");

$ch = curl_init('http://127.0.0.1/we/city/f0_1498');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);
*/
?>